package com.penghaisoft.microwms.product.web.controller.outwarehouse;

import com.penghaisoft.microwms.common.Constant;
import com.penghaisoft.microwms.common.controller.BaseController;
import com.penghaisoft.microwms.common.data.model.ProScrapOut;
import com.penghaisoft.microwms.common.data.model.RawHandOut;
import com.penghaisoft.microwms.common.dto.Resp;
import com.penghaisoft.microwms.common.dto.TransResult;
import com.penghaisoft.microwms.common.page.Pager;
import com.penghaisoft.microwms.common.service.IWmsCommonService;
import com.penghaisoft.microwms.product.service.IProScrapOutService;
import lombok.extern.slf4j.Slf4j;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping(value = "/proscrapout")
@Slf4j
public class ProScrapOutController extends BaseController {
	
		@Autowired
		private IProScrapOutService proScrapOutService;
		@Autowired
		private IWmsCommonService wmsCommonService;

	  /**
	   * 新增记录
	   * @param proScrapOut
	   * @return
	   */
		@PostMapping("c")
		public TransResult create(ProScrapOut proScrapOut) {
			proScrapOut.preInsert();
			Resp resp =  proScrapOutService.create(proScrapOut);
			return fromResp(resp);
		}

	  /**
	   * 删除记录（逻辑删除active_flag=0)
	   * @param proScrapOut
	   * @return
	   */
		@PostMapping("d")
		public TransResult delete(ProScrapOut proScrapOut) {
			proScrapOut.preUpdate();
			Resp resp =  proScrapOutService.delete(proScrapOut);
			return fromResp(resp);
		}

	  /**
	   * 查询列表
	   * @param page
	   * @return
	   */
		@PostMapping("list")
		public TransResult list(@RequestParam(name = "page", defaultValue = "1") int page,
                            @RequestParam(name = "rows", defaultValue = "10") int rows,ProScrapOut proScrapOut) {
			Pager<ProScrapOut> result = proScrapOutService.findListByCondition(page,rows,proScrapOut);
			return success(result);
		}

	  /**
	   * 查询单条
	   * @param id
	   * @return
	   */
		@GetMapping("r/{id}")
		public TransResult queryById(@PathVariable String id) {
			ProScrapOut  proScrapOut = proScrapOutService.findById(id);
			return success(proScrapOut);
		}

	  /**
	   * 修改记录
	   * @param proScrapOut
	   * @return
	   */
		@PostMapping("u")
		public TransResult update(ProScrapOut proScrapOut) {
			proScrapOut.preUpdate();
			Resp resp =  proScrapOutService.update(proScrapOut);
	    	return fromResp(resp);
		}
		
		/** 
		* @Title: proHandOutCreate 
		* @Description: 批量新增
		* @param @param list
		* @param @return  参数说明 
		* @return TransResult    返回类型
		* @author jzh 
		* @throws 
		*/
		@PostMapping("/batchInsert")
		public TransResult proHandOutCreate(@RequestBody List<ProScrapOut> list) {
			TransResult transResult = new TransResult();
			if(list == null || list.isEmpty()){
				transResult.setCode(Constant.FAIL);
				transResult.setMsg("请选择成品型号及数量！");
				return transResult;
			}else{
				//生成手工出库单号
				String scrapOutNo = wmsCommonService.getProShortSeq5(Constant.SEQ_PREFIX_SHORT_CSGC, Constant.SEQ_PREFIX_SHORT_REDIS_MAT_CSGC, 1).get(0);
				//批量创建手工出库任务
				Resp resp = proScrapOutService.insert(list,scrapOutNo, getLoginName());
				
				transResult.setCode(resp.getCode());
				transResult.setMsg(resp.getMsg());
			}
			
			return transResult;
		}
}
